﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TeamOPSchoolCup.Domain.POCO;
using TeamOPSchoolCup.Domain.DAL;
using TeamOPSchoolCup.Domain.Services;

namespace TeamOPSchoolCup.Web.Controllers
{
  public class ResultController : Controller
  {
    //
    // GET: /Result/

    public ViewResult Index()
    {
      EFDbContext db = new EFDbContext();

      List<Result> resultsByStudent = ResultService.GetResults(student:db.Students.Include("Member").Single(x => x.Member.Name == "S1"));
      List<Result> resultsByEvent = ResultService.GetResults(pEvent:db.Events.Single(x => x.Name == "100m baksteenslag"));
      List<Result> resultsByTeam = ResultService.GetResults(team:TeamService.GetTeams().First());
      ViewBag.ResultsByStudent = resultsByStudent;
      ViewBag.ResultsByEvent = resultsByEvent;
      ViewBag.ResultsByTeam = resultsByTeam;
      var results = db.Results.Include(r => r.Event).Include("TeamMembership.Student.Member");
      return View(results.ToList());
    }

    //
    // GET: /Result/Details/5

    public ViewResult Details(int id)
    {
      Result result = ResultService.GetResult(id);
      return View(result);
    }

    //
    // GET: /Result/Create

    public ActionResult Create()
    {
      ViewBag.EventId = new SelectList(EventService.GetEvents(), "EventId", "Name");
      return View();
    }

    //
    // POST: /Result/Create

    [HttpPost]
    public ActionResult Create(Result result)
    {
      if (ModelState.IsValid)
      {
        ResultService.AddResult(result);
        return RedirectToAction("Index");
      }

      ViewBag.EventId = new SelectList(EventService.GetEvents(), "EventId", "Name", result.EventId);
      return View(result);
    }

    //
    // GET: /Result/Edit/5

    public ActionResult Edit(int id)
    {
      Result result = ResultService.GetResult(id);
      ViewBag.EventId = new SelectList(EventService.GetEvents(), "EventId", "Name", result.EventId);
      return View(result);
    }

    //
    // POST: /Result/Edit/5

    [HttpPost]
    public ActionResult Edit(Result result)
    {
      if (ModelState.IsValid)
      {
        ResultService.UpdateResult(result);
        return RedirectToAction("Index");
      }
      ViewBag.EventId = new SelectList(EventService.GetEvents(), "EventId", "Name", result.EventId);
      return View(result);
    }
  }
}